package com.gudaichaoren.spider.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gudaichaoren.spider.constant.Constants;
import com.gudaichaoren.spider.entity.LotteriesShuangseAnalysis;
import com.gudaichaoren.spider.entity.LotteriesShuangseCalculate;
import com.gudaichaoren.spider.mapper.LotteriesShuangseAnalysisMapper;
import com.gudaichaoren.spider.mapper.LotteriesShuangseResultsMapper;
import com.gudaichaoren.spider.service.LotteriesShuangseAnalysisService;
import com.gudaichaoren.spider.service.LotteriesShuangseCalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 彩票_双色球分析数据 服务实现类
 * </p>
 *
 * @author 古代超人
 * @since 2020-09-29
 */
@Service
public class LotteriesShuangseAnalysisServiceImpl extends ServiceImpl<LotteriesShuangseAnalysisMapper, LotteriesShuangseAnalysis> implements LotteriesShuangseAnalysisService {

    @Autowired
    private LotteriesShuangseAnalysisMapper lotteriesShuangseAnalysisMapper;

    @Autowired
    private LotteriesShuangseResultsMapper lotteriesShuangseResultsMapper;

    @Autowired
    private LotteriesShuangseCalculateService lotteriesShuangseCalculateService;

    @Override
    public void Calculate(Constants.LotteriesShuangseAnalysisTypeEnum lotteriesShuangseAnalysisTypeEnum) {
        QueryWrapper<LotteriesShuangseAnalysis> analysisQueryWrapper = new QueryWrapper<LotteriesShuangseAnalysis>().eq("analysis_type", Constants.LotteriesShuangseAnalysisTypeEnum.当前遗漏.getCode()).last("limit 1");
        LotteriesShuangseAnalysis shuangseAnalysis = lotteriesShuangseAnalysisMapper.selectOne(analysisQueryWrapper);
        JSONObject jsonObject = JSONUtil.parseObj(shuangseAnalysis);
        //放入红球map
        HashMap<String, Integer> redConvertMap = new HashMap<>(33);
        HashMap<String, Integer> blueConvertMap = new HashMap<>(16);
        for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {
            if (stringObjectEntry.getKey().contains("red")) {
                redConvertMap.put(stringObjectEntry.getKey().substring(stringObjectEntry.getKey().length()-2), Integer.parseInt(stringObjectEntry.getValue().toString()));
            }
            if (stringObjectEntry.getKey().contains("blue")) {
                blueConvertMap.put(stringObjectEntry.getKey().substring(stringObjectEntry.getKey().length()-2), Integer.parseInt(stringObjectEntry.getValue().toString()));
            }
        }
        //通过当前遗漏算法算出红6球
        TreeSet<String> redResult = new TreeSet<>();
        while (true) {
            if (redResult.size()>5) {
                break;
            }
            redResult.add(weightRandom(redConvertMap).substring(weightRandom(redConvertMap).length() - 2));

        }
        //通过当前遗漏算法算出蓝1球
        TreeSet<String> blueResult = new TreeSet<>();
        while (true) {
            if (blueResult.size()>0) {
                break;
            }
            blueResult.add(weightRandom(blueConvertMap).substring(weightRandom(blueConvertMap).length() - 2));
        }
        //查询出当前期数
        int maxPeriod = lotteriesShuangseResultsMapper.selectMaxPeriod() + 1;
        //保存进计算结果表
        LotteriesShuangseCalculate lotteriesShuangseCalculate = new LotteriesShuangseCalculate().setRedBallNumber01(redResult.pollFirst()).setRedBallNumber02(redResult.pollFirst()).setRedBallNumber03(redResult.pollFirst()).setRedBallNumber04(redResult.pollFirst()).setRedBallNumber05(redResult.pollFirst()).setRedBallNumber06(redResult.pollFirst()).setBlueBallNumber01(blueResult.pollFirst()).setPeriod(maxPeriod+"");
        lotteriesShuangseCalculateService.save(lotteriesShuangseCalculate);

    }
    private String weightRandom(Map<String, Integer> map) {
        Set<String> keySet = map.keySet();
        List<String> weights = new ArrayList<String>();
        for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
            String weightStr = it.next();
            int weight =map.get(weightStr);
            //加10数据，防止当前为0的情况
            weight = weight + 10;
            for (int i = 0; i <= weight; i++) {
                weights.add(weightStr);
            }
        }
        int idx = RandomUtil.randomInt(weights.size());
        return weights.get(idx);
    }
}
